package net.midnighttd.game.view;

import net.midnighttd.engine.Engine;
import net.midnighttd.engine.entity.Entity;
import net.midnighttd.game.entity.Tower;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.LinkedList;

/**
 * Author: Adam Scarr
 * Date: 21/04/2009
 * Time: 11:40:00 AM
 */
public class TowerSelector extends JPanel implements ActionListener {
    private Tower activeTower = null;
    private EntityInspector entityInspector;
    private LinkedList<EntityButton> btns = new LinkedList<EntityButton>();
    private MainUI ui;

    public TowerSelector(MainUI ui, EntityInspector td) {
        this.ui = ui;
        setLayout(new FlowLayout());
        this.entityInspector = td;

        setBackground(new Color(106, 125, 94));
        setBorder(BorderFactory.createLineBorder(new Color(32, 29, 28)));

        reinit();
    }

    public void actionPerformed(ActionEvent e) {
        EntityButton btn = (EntityButton)e.getSource();
        activeTower = (Tower)btn.getEntity();
        btn.setStatus(EntityButton.ACTIVE);

        displayEntity(activeTower);

        for(EntityButton btn2: btns) {
            if(btn2 != btn) {
                btn2.setStatus(EntityButton.INACTIVE);
            }
        }
    }

    public Tower getActiveTower() {
        return activeTower;
    }

    public void reinit() {
        for(Entity e: ui.getGame().getEntityManager().getEntsOfType("tower")) {
            if(!((Tower)e).isUpgrade()) {
                EntityButton btn = new EntityButton(e);
                btn.addActionListener(this);
                add(btn);
                btns.add(btn);
            }
        }

        revalidate();
    }

    public void displayEntity(Entity entity) {
        entityInspector.displayEntityDetails(ui, entity);
    }
}
